import {forEach} from 'lodash';

export default {
  namespaced: true,

  state: {
    pages: [],
    tags: [],
  },

  mutations: {
    addPageAndTags (state, {payload}) {
      const {title, image, name, path, tags} = payload;

      state.pages.push({
        title, image, name, path, tags
      });

      forEach(tags, tag => {
        if (!state.tags.includes(tag)) {
          state.tags.push(tag);
        }
      });
    }
  },

  actions: {
    addPage ({commit}, {payload}) {

      const {title, image, name, path, tags} = payload;

      if (title && name && path) {
        commit({
          type: 'addPageAndTags',
          payload: {title, image, name, path, tags}
        });
      }
    },
  },
}
